import tkinter as tk
from tkinter import messagebox
import time
from datetime import datetime
from threading import Timer
import threading
from tkinter import ttk
from tkinter import *

class TodoApp:
    def __init__(self, master):
        self.master = master
        self.master.title("待办事项")

        # 初始化待办事项列表和搜索框
        self.todos = []
        self.search_var = tk.StringVar()

        # 主窗口布局
        self.create_widgets()

    def create_widgets(self):
        # 待办事项列表
        self.listbox = tk.Listbox(self.master, width=50, height=10)
        self.listbox.pack(pady=10)

        # 搜索栏
        search_entry = tk.Entry(self.master, textvariable=self.search_var, width=30)
        search_entry.pack()
        search_button = tk.Button(self.master, text="搜索", command=self.search_todo)
        search_button.pack()

        # 添加和编辑按钮
        add_button = tk.Button(self.master, text="添加", command=self.open_edit_window)
        add_button.pack()

    def open_edit_window(self):
        edit_window = tk.Toplevel(self.master)
        edit_window.title("Edit Todo")

        # 编辑页面的内容
        inputmessageyear = Label(edit_window, text="年份")  # 年份
        inputmessageyear.grid(column=0, row=0)
        inputyear = Spinbox(edit_window, from_=2022, to=3000)
        inputyear.grid(column=1, row=0)

        inputmessagemonth = Label(edit_window, text="月份")  # 月份
        inputmessagemonth.grid(column=0, row=1)
        inputmonth = Spinbox(edit_window, from_=1, to=12)
        inputmonth.grid(column=1, row=1)

        inputmessageday = Label(edit_window, text="日")  # 日
        inputmessageday.grid(column=0, row=2)
        inputday = Spinbox(edit_window, from_=1, to=31)
        inputday.grid(column=1, row=2)

        inputmessagehour = Label(edit_window, text="小时")  # 小时
        inputmessagehour.grid(column=0, row=3)
        inputhour = Spinbox(edit_window, from_=0, to=60)
        inputhour.grid(column=1, row=3)

        inputmessageminth = Label(edit_window, text="分钟")  # 分钟
        inputmessageminth.grid(column=0, row=4, )
        inputminth = Spinbox(edit_window, from_=0, to=60)
        inputminth.grid(column=1, row=4)

        todo_name_entry = tk.Entry(edit_window)
        todo_name_entry.grid(row=0, column=1)
        start_time_entry = tk.Entry(edit_window)
        start_time_entry.grid(row=1, column=1)
        end_time_entry = tk.Entry(edit_window)
        end_time_entry.grid(row=2, column=1)
        reminder_entry = tk.Entry(edit_window)
        reminder_entry.grid(row=3, column=1)

        save_button = tk.Button(edit_window, text="保存", command=lambda: self.save_todo(edit_window,
                                                                                         todo_name_entry.get(),
                                                                                         start_time_entry.get(),
                                                                                         end_time_entry.get(),
                                                                                         reminder_entry.get()))
        save_button.grid(row=4, column=0, columnspan=3)
        delete_button = tk.Button(edit_window, text="删除", command=lambda: self.delete_todo())
        delete_button.grid(row=4, column=2, columnspan=2)

    def save_todo(self, edit_window, todo_name, start_time, end_time, reminder):
        # 保存待办事项
        todo_item = {
            "name": todo_name,
            "start_time": start_time,
            "end_time": end_time,
            "reminder": reminder
        }
        self.todos.append(todo_item)
        self.update_listbox()
        edit_window.destroy()

    def delete_todo(self):
        # 删除待办事项
        if messagebox.askokcancel("删除", "你确定要删除这个事项吗?"):
            # 删除选定的事项
            selected_index = self.listbox.curselection()
            if selected_index:
                del self.todos[selected_index[0]]
                self.update_listbox()

    def search_todo(self):
        # 根据关键字搜索待办事项
        search_term = self.search_var.get().lower()
        filtered_todos = [todo for todo in self.todos if search_term in todo['name'].lower()]
        self.update_listbox(filtered_todos)

    def update_listbox(self, todos=None):
        # 更新列表框中的内容
        self.listbox.delete(0, tk.END)
        todos_to_display = todos if todos else self.todos
        for todo in todos_to_display:
            self.listbox.insert(tk.END, todo['name'])

    # 主程序
if __name__ == "__main__":
        root = tk.Tk()
        app = TodoApp(root)
        root.mainloop()


